package ying.cookie04;/**
 * ClassName: DeleteCookie
 * Package: ying.cookie04
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/10/5 10:09
 * @Version 1.0
 */

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import ying.utils.CookieUtils;

import java.io.IOException;

@WebServlet("/deleteCookie")
public class DeleteCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = CookieUtils.getCookie(request.getCookies(), "id");
        if (cookie != null) {
                cookie.setMaxAge(0);
                cookie.setPath(cookie.getPath());
                response.addCookie(cookie);
        }else {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("cookie不存在");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
